package com.quasar.hpatient.model.custommodel;

import com.quasar.hpatient.utils.AlarmTime;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class Alarm implements Serializable {

	private String uuid;
	public Date start;
	public Date end;
	public String name;
	public String uint;

	public String getUuid() {
		return uuid;
	}
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}
	public Alarm() {
		start = new Date();
		uuid = UUID.randomUUID().toString();
	}

	public List<AlarmTime> getTimes() {
		return times;
	}

	private List<AlarmTime> times = new ArrayList<AlarmTime>();

	public void clearList(){
		times.clear();
	}

	public void add(AlarmTime time) {
		times.add(time);
	}
	public void remove(AlarmTime time) {
		times.remove(time);
	}

	/**
	 * 1为一次     2为间隔多久 3为星期几
	 */
	public int type=2;

	public int day;
	public int index;
	public boolean isok;
    public  long getRepeat(){
		long result=day*86400000l;
		return  result;
	}
	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		for (AlarmTime alarmTime : times) {
			buffer.append(alarmTime.getTimeInfo());
			buffer.append("\n");
		}

		return buffer.toString();
	}

	public boolean isRun() {
		boolean result = false;
		return result;
	}

	public List<AlarmItem> getAlerms() {

		Calendar first = null;

		List<AlarmItem> result = new ArrayList<AlarmItem>();
		if (type == 1) {
			first=Calendar.getInstance();
		} else if (type == 2) {
			
			first = getFirstCalendar();
			

		}
		
		for (AlarmTime time : times) {
			AlarmItem item=new AlarmItem();
			item.setUuid(time.getUuid());
			item.time=time;
			item.day=day;
			item.repeat=day*86400000l;
			item.first=first;
			result.add(item);
		}
		
		return result;
	}

	private Calendar getFirstCalendar() {
		Calendar now = Calendar.getInstance();
		now.set(Calendar.HOUR_OF_DAY,0);
		now.set(Calendar.MINUTE,0);
		now.set(Calendar.SECOND,0);
		Calendar first = Calendar.getInstance();
		first.setTime(start);
		if (first.after(now)) {
			
		} else {
			long time = now.getTimeInMillis() - start.getTime();
			long days = time / 86400000;
			//int day = 7;
			int x = day+(int) (days / day)*day;
			first.add(Calendar.DAY_OF_MONTH, x);
		}
		return first;
	}

	public long repeat() {

		long result = 86400000 * day;

		return result;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		Alarm alarm = (Alarm) o;

		return !(uuid != null ? !uuid.equals(alarm.uuid) : alarm.uuid != null);

	}

	@Override
	public int hashCode() {
		return uuid != null ? uuid.hashCode() : 0;
	}
}
